package org.bozo.im.service.protocols.jabber;

import org.bozo.im.service.IMSession;
import org.bozo.im.service.IMSessionConfiguration;
import org.bozo.im.service.protocols.xmpp.util.jid.JID;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;

public class JabberSession extends IMSession {

	private IMSessionConfiguration mConfiguration;
	private XMPPConnection mConnection;
	private JabberRosterListener mRosterListener;

	public JabberSession(IMSessionConfiguration pConfiguration) {
		super(pConfiguration);
		mConfiguration = pConfiguration;
	}

	@Override
	public IMSessionConfiguration getConfiguration() {
		return mConfiguration;
	}

	@Override
	public void startSession() throws Exception {
		
		JID lJid = new JID(mConfiguration.getLogin());
		
		ConnectionConfiguration lConfiguration = new ConnectionConfiguration(mConfiguration.getServer(), mConfiguration.getPort(), lJid.getDomain());
		mConnection = new XMPPConnection(lConfiguration);
		mConnection.connect();
		mConnection.login(lJid.getLoginName(), mConfiguration.getPassword());
		
		mRosterListener = new JabberRosterListener(mConfiguration, mConnection.getRoster());
	}

	@Override
	public void stopSession() throws Exception {
		mConnection.disconnect();
	}

}
